package jtk.project4.fleet.screen.addAttachment;

import java.awt.FlowLayout;
import java.util.List;

import jtk.project4.fleet.action.CancelAction;
import jtk.project4.fleet.action.CancelAddAttachmentAction;
import jtk.project4.fleet.action.SaveAddAttachmentAction;
import jtk.project4.fleet.action.SubmitAction;
import jtk.project4.fleet.event.AddAttachmentEvent;
import jtk.project4.fleet.event.AddMaintenanceTaskEvent;
import jtk.project4.fleet.event.EquipmentEvent;
import jtk.project4.fleet.field.AddAttachmentField;
import jtk.project4.fleet.field.EquipmentInsurancePolicyInformationField;
import jtk.project4.fleet.field.IdentificationField;
import nl.coderight.jazz.action.Action;
import nl.coderight.jazz.dialog.MessageType;
import nl.coderight.jazz.dialog.OptionDialog;
import nl.coderight.jazz.dialog.OptionType;
import nl.coderight.jazz.dialog.ReturnOptionType;
import nl.coderight.jazz.form.FormHeader;
import nl.coderight.jazz.form.FormLayout;
import nl.coderight.jazz.form.FormView;
import nl.coderight.jazz.form.FormLayout.Align;
import nl.coderight.jazz.form.control.GroupControl;
import nl.coderight.jazz.form.field.MemoField;
import nl.coderight.jazz.form.field.TextField;
import nl.coderight.jazz.form.field.button.PushButton;
import nl.coderight.jazz.form.validation.ValidationError;

public class AddAttachmentView extends FormView {
	private PushButton submitButton;
	private PushButton cancelButton;
	private Action submitAction;
	private Action cancelAction;	
		
		
	public void handleEvent(AddAttachmentEvent evt) {
		switch (evt.getType()) {
		case CANCEL:
			close();
			break;
			
		case SAVE:
		
			// check for validation errors
			List<ValidationError> errors = validate();
			if (!errors.isEmpty()) {
				// there are validation errors so do not submit...
				errors.get(0).getField().focus();
				break;
			} else {
				// submit form and close view
				submit();
				close();
				// fall-through!
			}
			
		default:
			// propagate event to controller
			propagateEvent(evt);
			break;
		}
	}
	
		@Override
		protected void init() {
			createAction();
			createFields();
			createLayout();		
		}	
		
		private void createAction() {
			submitAction = new SubmitAction();
			cancelAction = new CancelAction();
			// enable submit button when form is changed
			setOnChangeAction(submitAction);
		}
		
		private void createFields(){
			submitButton = new PushButton(new SaveAddAttachmentAction());
			cancelButton = new PushButton(new CancelAddAttachmentAction());
		}
		
		private void createLayout() {
			//setHeader(createHeader());
			addField(createForm());			
		}
		
		/*private FormHeader createHeader() {
			formHeader = new FormHeader();
			formHeader.setTitle("equipment.header.form");
			return formHeader;
		}
		*/
		private GroupControl createForm() {
			GroupControl form = new GroupControl();
			//form.addField(selectButton, NORTH);
			form.addField(new AddAttachmentField("attachment"), NORTH);
			form.addField(createButtons(), SOUTH);		
			return form;
		}
		
		private GroupControl createButtons() {
			GroupControl buttons = new GroupControl();
			buttons.setLayout(new FlowLayout(FlowLayout.RIGHT));
			buttons.addField(submitButton);
			buttons.addField(cancelButton);
			return buttons;
		}	

	}


